https://leetcode.com/problems/plus-one/
給一個陣列把最後一個數字加一,如需要進位把該數字變成零下一進位值加一。
判斷該數字是否大於九需要進位,如需要進位把該數字變為0之後判斷下一個數字是否也需要進位以此循環。如循環到最後一個數字也該進位時,則須建立一個新的陣列來儲存。
int* plusOne(int* digits, int digitsSize, int* returnSize) {
if (digits == NULL)
{
return NULL;
}
int n = digitsSize-1;
while(n >= 0)
{
if(digits[n] < 9)
{
digits[n]++;
*returnSize = digitsSize;
return digits;
}else
{
digits[n] = 0;
n--;
}
}
int* newdigit = (int*)malloc((digitsSize+1) * sizeof(int));
newdigit[0] = 1;
for(int i = 1; i < (digitsSize+1); i++)
{
newdigit[i] = digits[i-1];
}
*returnSize = digitsSize+1;
return newdigit;
}
var plusOne = function(digits) {
var result = [];
var n = digits.length -1 ;
while(n >= 0)
{
if(digits[n] < 9)
{
digits[n]++;
return digits;
}
else
{
digits[n] = 0;
n--;
}
}
result[0] = 1;
for(var i = 0; i<digits.length; i++)
{
result[i+1] = digits[i];
}
return result;
};
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
If you can’t give up on what you love, you must work harder to let others understand.
如果對喜歡的事情沒有辦法放棄,那就要更努力地讓別人看見你的存在